package com.net.wanjian.networkhospitalmanager.utils;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.view.WindowManager;

/**
 * Description:
 * Created by lidongcai on 17/5/9.
 * Email:  lidongcaiocc@163.com
 */

public class ProgressDialogUtils {

    private static ProgressDialog _sProgressDialog;

    public interface OnClickDialogBtnListenner {
        /**
         * 确认按钮ID
         */
        public static final int CONFIRM_BUTTON_ID = 0x01;
        /**
         * 取消按钮ID
         */
        public static final int CANCEL_BUTTON_ID = CONFIRM_BUTTON_ID + 1;

        /**
         * 对话框按钮点击事件
         *
         * @param btnId
         *            被点击的按钮ID
         */
        public void onClickDialogBtn(int btnId);
    }

    /**
     * 显示只有确认按钮的提示对话框
     *
     * @param context
     * @param titleResId
     *            标题文本字符串资源ID
     * @param infoResId
     *            提示信息文本字符串资源ID
     * @param confirmBtnTextResId
     *            确认按钮文本字符串资源ID
     * @param listenner
     *            按钮点击事件监听器
     * @param cancelable
     *            是否可以按BACK按钮关闭对话框
     */
    public static void showConfirmDialog(Context context, int titleResId,
                                         int infoResId, int confirmBtnTextResId,
                                         OnClickDialogBtnListenner listenner, boolean cancelable) {
        try {
            showConfirmDialog(context, context.getString(titleResId),
                    context.getString(infoResId),
                    context.getString(confirmBtnTextResId), listenner,
                    cancelable);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 显示只有确认按钮的提示对话框
     *
     * @param context
     * @param title
     *            标题文本字符串
     * @param info
     *            提示信息文本字符串
     * @param confirmBtnText
     *            确认按钮文本字符串
     * @param listenner
     *            按钮点击事件监听器
     * @param cancelable
     *            是否可以按BACK按钮关闭对话框
     */
    public static void showConfirmDialog(Context context, String title,
                                         String info, String confirmBtnText,
                                         final OnClickDialogBtnListenner listenner, boolean cancelable) {
        try {
            AlertDialog.Builder builder = new AlertDialog.Builder(context);
            builder.setTitle(title);
            builder.setMessage(info);
            builder.setCancelable(cancelable);
            builder.setPositiveButton(confirmBtnText, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    if (listenner != null) {
                        listenner
                                .onClickDialogBtn(OnClickDialogBtnListenner.CONFIRM_BUTTON_ID);
                    }
                }
            });
            AlertDialog create = builder.create();
            create.getWindow().setType(
                    WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
            create.show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 显示有确认按钮和取消按钮的询问对话框
     *
     * @param context
     * @param titleResId
     *            标题文本字符串资源ID
     * @param infoResId
     *            提示信息文本字符串资源ID
     * @param confirmBtnTextResId
     *            确认按钮文本字符串资源ID
     * @param cancelBtnTextResId
     *            取消按钮文本字符串资源ID
     * @param listenner
     *            按钮点击事件监听器
     * @param cancelable
     *            是否可以按BACK按钮关闭对话框
     */
    public static void showAskDialog(Context context, int titleResId,
                                     int infoResId, int confirmBtnTextResId, int cancelBtnTextResId,
                                     OnClickDialogBtnListenner listenner, boolean cancelable) {
        try {
            showAskDialog(context, context.getString(titleResId),
                    context.getString(infoResId),
                    context.getString(confirmBtnTextResId),
                    context.getString(cancelBtnTextResId), listenner,
                    cancelable);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 显示有确认按钮和取消按钮的询问对话框
     *
     * @param context
     * @param title
     *            标题文本字符串
     * @param info
     *            提示信息文本字符串
     * @param confirmBtnText
     *            确认按钮文本字符串
     * @param cancelBtnText
     *            取消按钮文本字符串
     * @param listenner
     *            按钮点击事件监听器
     * @param cancelable
     *            是否可以按BACK按钮关闭对话框
     */
    public static void showAskDialog(Context context, String title,
                                     String info, String confirmBtnText, String cancelBtnText,
                                     final OnClickDialogBtnListenner listenner, boolean cancelable) {
        try {
            AlertDialog.Builder builder = new AlertDialog.Builder(context);
            builder.setTitle(title);
            builder.setMessage(info);
            builder.setCancelable(cancelable);
            builder.setPositiveButton(confirmBtnText, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    if (listenner != null) {
                        listenner
                                .onClickDialogBtn(OnClickDialogBtnListenner.CONFIRM_BUTTON_ID);
                    }
                }
            });
            builder.setNegativeButton(cancelBtnText, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    if (listenner != null) {
                        listenner
                                .onClickDialogBtn(OnClickDialogBtnListenner.CANCEL_BUTTON_ID);
                    }
                }
            });
            builder.create().show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 显示对话框
     *
     * @param context
     * @param title
     *            标题文本字符串,如果为NULL则不显示标题
     * @param message
     *            消息文本字符串
     */
    public static void showProgressDialog(Context context, String title,
                                          String message) {
        try {
            if (_sProgressDialog != null && _sProgressDialog.isShowing()) {
                _sProgressDialog.dismiss();
            }
            _sProgressDialog = new ProgressDialog(context);
            if (title != null) {
                _sProgressDialog.setTitle(title);
            }
            _sProgressDialog.setMessage(message);
            _sProgressDialog.setCancelable(true);
            _sProgressDialog.show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 显示阻塞进度对话框
     *
     * @param context
     * @param title
     *            标题文本字符串,如果为NULL则不显示标题
     * @param messageId
     *            消息文本字符串
     */
    public static void showProgressDialog(Context context, String title,
                                          int messageId) {
        try {
            String message = context.getString(messageId);
            if (_sProgressDialog != null && _sProgressDialog.isShowing()) {
                _sProgressDialog.dismiss();
            }
            _sProgressDialog = new ProgressDialog(context);
            if (title != null) {
                _sProgressDialog.setTitle(title);
            }
            _sProgressDialog.setMessage(message);
            _sProgressDialog.setCancelable(false);
            _sProgressDialog.show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 关闭阻塞进度对话框
     */
    public static void dismissProgressDialog() {
        try {
            if (_sProgressDialog != null && _sProgressDialog.isShowing()) {
                _sProgressDialog.dismiss();
            }
            _sProgressDialog = null;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     *
     *
     * @return
     */
    public static void setCancelable(boolean flag) {
        if (_sProgressDialog != null && _sProgressDialog.isShowing()) {
            _sProgressDialog.setCancelable(flag);
        }
    }
}
